package com.hyts.gateway.dal.config; /**
 * Copyright [2020] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.google.common.base.Splitter;
import com.hyts.gateway.base.context.TenantNoContextHolder;
import com.hyts.gateway.base.enums.PropertiesEnum;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @project-name:gateway
 * @package-name:PACKAGE_NAME
 * @author:LiBo/Alex
 * @create-date:2020-12-27 19:24
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description: mybatis框架的拦截器以及相关的配置服务
 */
@Configuration
@MapperScan("com.hyts.gateway.dao.mapper")
public class GwDalConfiguration {


    Splitter splitter = Splitter.onPattern(PropertiesEnum.IGNORE_TABLE_LIST.code()).trimResults().omitEmptyStrings();


    @Autowired
    TenantNoContextHolder tenantNoContextHolder;

    /**
     * 分页条件的拦截器
     * @return
     */
    @Bean
    public PaginationInnerInterceptor paginationInnerInterceptor(){
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        paginationInnerInterceptor.setMaxLimit(Long.MAX_VALUE);
        return paginationInnerInterceptor;
    }

    /**
     * 租户内部拦截器
     * @return
     */
    @Bean
    public TenantLineInnerInterceptor tenantLineInnerInterceptor(){
        TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();
        tenantLineInnerInterceptor.setTenantLineHandler(new TenantLineHandler() {

            @Override
            public Expression getTenantId() {
                return new StringValue(tenantNoContextHolder.getTenantNo());
            }

            @Override
            public String getTenantIdColumn() {
                return PropertiesEnum.TENANT_ID.code();
            }

            @Override
            public boolean ignoreTable(String tableName) {
                return splitter.splitToStream(PropertiesEnum.IGNORE_TABLE_LIST.value()).
                        anyMatch((e) -> e.equalsIgnoreCase(tableName));
            }
            });
        return tenantLineInnerInterceptor;
    }

}
